package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessRealTimePrice;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.view.ChannelView.ChannelActivity;
import com.digitalchina.lxnsy.view.ChannelView.ChannelEntity;
import com.digitalchina.lxnsy.view.ChannelView.PriceFragment;
import com.digitalchina.lxnsy.xml.ChannelXML;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.weyye.library.colortrackview.ColorTrackTabLayout;

import static com.digitalchina.lxnsy.constant.MSG.MSG_GET_PRODUCT_TYPE_FIELD;
import static com.digitalchina.lxnsy.constant.MSG.MSG_GET_PRODUCT_TYPE_SUCCESS;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：顶部有tab切换的
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/7/22 19:58
 * 修改备注：
 * ================================================
 */
public class RealTimePriceTabActivity extends BaseActivity implements PriceFragment.CallBackValue {


    @Bind(R.id.tab)
    ColorTrackTabLayout tab;
    @Bind(R.id.image_add)
    ImageView imageAdd;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.tab_tv_update_time)
    TextView tabTvUpdateTime;

    private List<ChannelEntity> entityList = new ArrayList<>(); // 获取数据
    private List<ChannelEntity> myList = new ArrayList<>(); // 展示数据
    private List<ChannelEntity> otherList = new ArrayList<>(); // 其他数据
    private List<Fragment> fragments = new ArrayList<>();
    private PriceFragment priceFragment;
    private Map<String, List<ChannelEntity>> map = new HashMap<>();
    private ChannelEntity currentChannel = new ChannelEntity(); // 当前
    private MyPagerAdapter adapter;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_realtime_price_tab);
        setTitle("实时价格");
    }

    @Override
    public void initMember() {

        adapter = new MyPagerAdapter(getSupportFragmentManager(), fragments, myList);
        viewPager.setOffscreenPageLimit(5);
        viewPager.setAdapter(adapter);

        tab.setSelectedTabIndicatorHeight(Utils.dip2px(this, 2));
        //设置每个Tab的内边距
        tab.setTabPaddingLeftAndRight(20, 20);
        tab.setupWithViewPager(viewPager);

        BusinessRealTimePrice.getProductType(this, mHandler);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 999) {
            myList.clear();
            fragments.clear();
            viewPager.removeAllViews();
            viewPager.removeAllViewsInLayout();

            String s = "";
            myList = (List<ChannelEntity>) data.getSerializableExtra("myChannelList");
            for (int i = 0; i < myList.size(); i++) {
                priceFragment = new PriceFragment();
                fragments.add(priceFragment);
                s += myList.get(i).getCode() + ",";
                Bundle bundle = new Bundle();
                bundle.putString("code", myList.get(i).getCode());
                priceFragment.setArguments(bundle);
            }

            ChannelXML.setMyChannel(this, s);

            adapter.setFragmentList(fragments, myList);
        }

    }

    @Override
    public void addListener() {


    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG_GET_PRODUCT_TYPE_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG_GET_PRODUCT_TYPE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        entityList = FastJsonUtil.getListBean(object.toString(), "productTypeList", ChannelEntity.class);

        // 判断本地是否有数据  如果有则按照本地数据展示  否则按照默认数据前十条
        String codes = ChannelXML.getMyChannel(this);
        if (!StringUtil.isStrEmpty(codes)) {
            myList.clear();
            for (int i = 0; i < entityList.size(); i++) {
                for (int j = 0; j < codes.split(",").length; j++) {
                    if (codes.split(",")[j].equals(entityList.get(i).getCode()))
                        myList.add(entityList.get(i));
                    else
                        otherList.add(entityList.get(i));
                }
            }
        } else {
            if (entityList != null) {
                String heatCode = FastJsonUtil.getString(object.toString(), "heatCode");
                for (int i = 0; i < entityList.size(); i++) {
                    for (int j = 0; j < heatCode.split(",").length; j++) {
                        if (heatCode.split(",")[j].equals(entityList.get(i).getCode()))
                            myList.add(entityList.get(i));
                        else
                            otherList.add(entityList.get(i));
                    }
                }
                ChannelXML.setMyChannel(this, heatCode);
            }
        }


        for (int i = 0; i < myList.size(); i++) {
            priceFragment = new PriceFragment();
            fragments.add(priceFragment);
            Bundle bundle = new Bundle();
            bundle.putString("code", myList.get(i).getCode());
            priceFragment.setArguments(bundle);
        }

        adapter.setFragmentList(fragments, myList);

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tab, R.id.image_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tab:
                break;
            case R.id.image_add:
                map.put("myList", myList);
                otherList.clear();
                otherList.addAll(entityList);
                for (int i = 0; i < myList.size(); i++) {
                    for (int j = 0; j < otherList.size(); j++) {
                        if (myList.get(i).getCode().equals(otherList.get(j).getCode())) {
                            otherList.remove(j);
                            j--;
                        }
                    }
                }
                map.put("otherList", otherList);
                GotoUtil.gotoForResultActivity(RealTimePriceTabActivity.this, ChannelActivity.class, 888, "map", map);
                break;
        }
    }

    //要获取的值  就是这个参数的值
    public void SendMessageValue(String strValue) {
        // TODO Auto-generated method stub
        tabTvUpdateTime.setText(strValue);
    }


    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> fragments = null;
        private List<ChannelEntity> channelEntityList = null;

        public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments, List<ChannelEntity> channelEntityList) {
            super(fm);
            this.fragments = fragments;
            this.channelEntityList = channelEntityList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return channelEntityList.get(position).getName();
        }

        public void setFragmentList(List<Fragment> pList, List<ChannelEntity> channelEntityList) {
            this.fragments = pList;
            this.channelEntityList = channelEntityList;
            notifyDataSetChanged();
        }

        @Override
        public int getItemPosition(Object object) {
            // 最简单解决 notifyDataSetChanged() 页面不刷新问题的方法
            return POSITION_NONE;
        }
    }

}
